package com.sptus.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sptus.aop.AppIntercept;
import com.sptus.entity.SptUser;
import com.sptus.entity.SptUserParent;
import com.sptus.service.ISptUserParentService;
import com.sptus.service.ISptUserService;
import com.sptus.vo.Result;
import com.sptus.vo.app.AppUserInvitationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "app分享账号")
@RequestMapping("/sys/app/userParent")
public class UserParentController {
    @Autowired
    ISptUserParentService sptUserParentService;

    @Autowired
    ISptUserService sptUserService;
    @GetMapping("/getUserGrade")
    @ApiOperation(value="获取用等级", notes="")
    @AppIntercept
    public Result<Object> getUserGrade(SptUser user ){
        Result<Object> result = new Result<>();
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        Integer id = user.getId();
        //获取邀请列表
        LambdaQueryWrapper<SptUserParent> sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,id);
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"1");
        List<HashMap<String,String>> sptUserParents = sptUserParentService.selectListInfo(sptUserParentLambdaQueryWrapper);
        if(sptUserParents.size()>0){
            stringStringHashMap.put("userGrade","2");
            stringStringHashMap.put("userParentEmail",sptUserParents.get(0).get("email"));
            stringStringHashMap.put("userParentId",sptUserParents.get(0).get("parent_id"));
            stringStringHashMap.put("InvitationList",new ArrayList<>());
        }else {
            sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            stringStringHashMap.put("userGrade","1");
            sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,id);
            stringStringHashMap.put("userParentEmail","");
            sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"0");
            sptUserParents = sptUserParentService.selectListInfo(sptUserParentLambdaQueryWrapper);
            stringStringHashMap.put("userParentId","");
            stringStringHashMap.put("invitationList",sptUserParents);
        }
        result.setResult(stringStringHashMap);
        result.setCode(200);
        return result;
    }
    @GetMapping("/getUserChild")
    @ApiOperation(value="获取用户下级用户列表", notes="")
    @AppIntercept
    public Result<Object> getUserChild( SptUser user ){
        Result<Object> result = new Result<>();
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        Integer id = user.getId();
        //获取邀请列表
        LambdaQueryWrapper<SptUserParent> sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getParentId,id);
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"1");
        List<HashMap<String,String>> sptUserParents = sptUserParentService.selectChildListInfo(sptUserParentLambdaQueryWrapper);
        stringStringHashMap.put("userChild",sptUserParents);
        result.setResult(stringStringHashMap);
        result.setCode(200);
        return result;
    }
    @PostMapping("/invitationAgree")
    @ApiOperation(value="同意邀请", notes="")
    @AppIntercept
    public Result<Object> invitationAgree(SptUser user,@RequestBody AppUserInvitationVo vo ){
        Result<Object> result = new Result<>();
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        Integer id = user.getId();
        //获取邀请列表
        LambdaQueryWrapper<SptUserParent> sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,id);
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getParentId,vo.getUserId());
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"0");
        SptUserParent sptUserParent = new SptUserParent();
        sptUserParent.setState(1);
        sptUserParent.setUpateTime(new Date());
        if(vo.getStatus().equals("1")){
            sptUserParentService.update(sptUserParent,sptUserParentLambdaQueryWrapper);
            sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,id);
            sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"0");
            sptUserParentService.remove(sptUserParentLambdaQueryWrapper);
        }else {
            sptUserParentService.remove(sptUserParentLambdaQueryWrapper);
        }
        result.setResult(stringStringHashMap);
        result.setCode(200);
        return result;
    }
    @PostMapping("/invitationDel")
    @ApiOperation(value="移除", notes="")
    @AppIntercept
    public Result<Object> invitationDel(SptUser user,@RequestBody AppUserInvitationVo vo ){
        Result<Object> result = new Result<>();
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        Integer id = user.getId();
        //获取邀请列表
        LambdaQueryWrapper<SptUserParent> sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getParentId,id);
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,vo.getUserId());
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"1");
        sptUserParentService.remove(sptUserParentLambdaQueryWrapper);
        result.setResult(stringStringHashMap);
        result.setCode(200);
        return result;
    }
    @PostMapping("/invitationUser")
    @ApiOperation(value="邀请用户", notes="")
    @AppIntercept
    public Result<Object> invitationUser(SptUser user,@RequestBody AppUserInvitationVo vo ){
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        Integer id = user.getId();
        Result<Object> result = new Result<>();
        if(ObjectUtils.isEmpty(vo.getEmail())){
            return result.error500("Please enter email\n" +
                    "\n");
        }

        //判断用户是否存在
        LambdaQueryWrapper<SptUser> sptUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserLambdaQueryWrapper.eq(SptUser::getEmail,vo.getEmail());
        SptUser sptUser = sptUserService.getBaseMapper().selectOne(sptUserLambdaQueryWrapper);
        if(ObjectUtils.isEmpty(sptUser)){
            stringStringHashMap.put("status","3");
            result.setMessage("Mailbox does not exist");
            result.setResult(stringStringHashMap);
            result.setCode(200);
            return result;
        }

        //判断邀请账号是否为一级账号
        LambdaQueryWrapper<SptUserParent> sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,sptUser.getId());
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"1");
        List<HashMap<String,String>> sptUserParents = sptUserParentService.selectListInfo(sptUserParentLambdaQueryWrapper);
        if(sptUserParents.size()>0){
            stringStringHashMap.put("status","1");
            result.setMessage("The invitation user is a secondary user");
            result.setResult(stringStringHashMap);
            result.setCode(200);
            return result;
        }


        //判断是否为主账号是否为一级用户
        sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,user.getId());
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"1");
        sptUserParents = sptUserParentService.selectListInfo(sptUserParentLambdaQueryWrapper);
        if(sptUserParents.size()>0){
            stringStringHashMap.put("status","2");
            result.setMessage("The user is a secondary user");
            result.setResult(stringStringHashMap);
            result.setCode(200);
            return result;

        }




        SptUserParent sptUserParent = new SptUserParent();
        sptUserParent.setParentId(id+"");
        sptUserParent.setUserId(sptUser.getId()+"");
        sptUserParent.setState(0);

        sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getParentId,id);
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,sptUser.getId());
        List<SptUserParent> sptUserParents1 = sptUserParentService.getBaseMapper().selectList(sptUserParentLambdaQueryWrapper);
        if(sptUserParents1.size()>0){
            stringStringHashMap.put("status","4");
            result.setMessage("The user has been invited");
            result.setResult(stringStringHashMap);
            result.setCode(200);
            return result;
        }
        sptUserParentService.save(sptUserParent);
        stringStringHashMap.put("status","0");
        result.setResult(stringStringHashMap);
        result.setCode(200);
        return result;
    }


}
